package xyz.qduwo.servlet.manager;

import xyz.qduwo.dao.UserDao;
import xyz.qduwo.entity.UserEntity;
import xyz.qduwo.util.EncryptUtils;
import xyz.qduwo.util.RespUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.http.HttpSession;

@WebServlet("/admin/manager/Login")
public class LoginServlet extends HttpServlet {
    UserEntity user;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        user = UserDao.getByUserName(username);
        resp.setContentType("application/x-json;charset=utf-8");

        if(user.getPassword().equals(EncryptUtils.stringToMD5(password))){
            resp.getWriter().print(RespUtils.success());
            HttpSession session = req.getSession(true);
            session.setAttribute("user", username);
            session.setAttribute("userentity",user);
        }else{
            resp.getWriter().print(RespUtils.fail("用户名或密码错误"));
        }
    }
}
